package gl.android.biyiqing.competent;

import gl.API;
import gl.android.biyiqing.R;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.Tip;
import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.alipay.sdk.pay.demo.AliPayUtil;

public class PayView {
	private View Pay;
	private OnClickListener payimp;
	private Activity context;
	
	@SuppressLint("InflateParams")
	public PayView(Activity context, View.OnClickListener payimp) {
		this.context=context;
		Pay = LayoutInflater.from(context).inflate(R.layout.pay, null);
		this.payimp = payimp;
		bindViews();
		
	}

	private View findViewById(int id) {
		return Pay.findViewById(id);
	}

	// Content View Elements

	private RelativeLayout mTopbar;
	private LinearLayout mTitle_back;
	private gl.android.widget.radio.RoundRadioGroup mGp_miane;
	private RadioButton mRadio_10;
	private RadioButton mRadio_20;
	private RadioButton mRadio2;
	private EditText mInput;
	private ImageView mAlipay;
	private ImageView mBank;
	private Button mPay;

	// End Of Content View Elements

	private void bindViews() {

		mTopbar = (RelativeLayout) findViewById(R.id.topbar);
		mTitle_back = (LinearLayout) findViewById(R.id.title_back);
		mGp_miane = (gl.android.widget.radio.RoundRadioGroup) findViewById(R.id.gp_miane);
		mRadio_10 = (RadioButton) findViewById(R.id.radio_10);
		mRadio_20 = (RadioButton) findViewById(R.id.radio_20);
		mRadio2 = (RadioButton) findViewById(R.id.radio2);
		mInput = (EditText) findViewById(R.id.input);
		mInput.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mGp_miane.clearCheck();
			}
		});
		mAlipay = (ImageView) findViewById(R.id.alipay);
		mAlipay.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(final View v) {
				
				float am = 0;
				if (!TextUtils.isEmpty(mInput.getText())) {
					am = (Float.parseFloat(mInput.getText().toString()));
					if (am <= 0) {
						Tip.show("金额不能小于0");
						return;
					}
				} else {
					am = getValue(mGp_miane.getCheckedRadioButtonId());
				}
				if (am <= 0) {
					Tip.show("金额不能小于0");
					return;
				}
				final float am1 = am;
				
				BaseControl.get(API.URL+"GetAlipayOrderNo", new AjaxCallBack<String>() {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							final String orderID = new JSONObject(t).getString("msg");
							
							//http://116.211.116.168:8080/by_alipaycallback/servlet/RSANotifyReceiver
							String body=String.format("%s#%s#%s",UserManger.getInstance().userid,am1,"android");
							String noticeUrl="http://" + API.HOST + ":" +API. PORT + "/"+"by_alipaycallback/servlet/RSANotifyReceiver?userid="+UserManger.getInstance().userid+"&amount="+am1+"&coin="+am1+"&from=android&orderno="+orderID;
							AliPayUtil mAliPayUtil=new AliPayUtil(context, new AliPayUtil.PayCallBack(){

								@Override
								public void onSuccess() {
									v.setTag(am1);
									Tip.show("充值成功");
									payimp.onClick(v);
									
								}

								@Override
								public void onFail(String msg) {
									Tip.show(msg);
								}
								
							}, orderID, "充值", body, ""+am1, noticeUrl);
							mAliPayUtil.pay(v);
						} catch (Exception e) {
							e.printStackTrace();
							Tip.show("获取订单失败");
						}
						
					}
					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						Tip.show(strMsg);
					}
				}, new Object());

				
			}
		});
		mBank = (ImageView) findViewById(R.id.bank);
		mBank.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Tip.show("暂时没有开放");
			}
		});
		
		mPay = (Button) findViewById(R.id.pay);
		mPay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				float am = 0;
				if (!TextUtils.isEmpty(mInput.getText())) {
					am = (Float.parseFloat(mInput.getText().toString()));
					if (am <= 0) {
						return;
					}
				} else {
					am = getValue(mGp_miane.getCheckedRadioButtonId());
				}
				if (am <= 0) {
					return;
				}
				view.setTag(am);
				payimp.onClick(view);

			}
		});
	}

	protected int getValue(int checkedRadioButtonId) {
		for (int i = 0; i < mGp_miane.getChildCount(); i++) {
			View child = mGp_miane.getChildAt(i);
			if (child instanceof RadioButton) {
				if (child.getId() == checkedRadioButtonId) {
					switch (i) {
					case 0:
						return 10;
					case 1:
						return 20;
					case 2:
						return 50;
					case 3:
						return 100;

					}
				}
			}
		}

		return -1;
	}
	public View getView() {
		return Pay;
 
	}
}
